package models;
import java.text.Format;
import java.text.ParseException;
import java.util.Date;
import javax.persistence.*;
import com.avaje.ebean.ExpressionList;
import com.avaje.ebean.Page;
import play.data.format.Formats;
import play.data.validation.Constraints;
import play.db.ebean.Model;
import controllers.JsonDateFormatter;
import controllers.JsonDatetimeFormatter;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import controllers.Helper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import play.libs.Json;
import models.Customer;
import models.Driver;
import models.Vehicle;
@Entity
@Table(name="check_work")
public class Check_work extends Model {
	private static final long serialVersionUID = 1L;
	@Id
	public Integer id; //编号
	
	public Integer drive_time; //用车时间
	@ManyToOne
	public Customer customer; //用车客户
	
	public String road_line; //计划行程
	@ManyToOne
	public Driver driver; //司机
	@ManyToOne
	public Vehicle vehicle; //车号
	
	public Integer check_time; //核定数（小时）
	public static Model.Finder<Integer, Check_work> find = new Model.Finder<Integer, Check_work>(Integer.class, Check_work.class);

	public static Page<Check_work> page(int page, int pageSize, String sortBy, String order, String [] filterColum, String[] filter) {
		ExpressionList<Check_work> exp = find.where();
		for(int i= 0; i< filterColum.length; i++) {
			String[] tokens = filter[i].split("\\|", 3);
			if (tokens.length == 0) {
				continue;
			}
			String fieldtype = tokens[0];
			if (tokens.length == 3) { //two values, range search
				if (tokens[1].isEmpty() || tokens[2].isEmpty() ) {
					continue;
				}
				Object obj0 = Helper.stringToObject(fieldtype, tokens[1]);
				Object obj1 = Helper.stringToObject(fieldtype, tokens[2]);
				exp = exp.between(filterColum[i], obj0, obj1);
			} else if (tokens.length == 2) { //one value search
				if (tokens[1].isEmpty()) {
					continue;
				}
				if (fieldtype.equalsIgnoreCase("String")) {
					exp = exp.ilike(filterColum[i], "%" + tokens[1] + "%");
				} else {
					Object obj0 = Helper.stringToObject(fieldtype, tokens[1]);
					exp = exp.eq(filterColum[i], obj0);
				}
			}
		}
		return exp.orderBy(sortBy + " " + order).findPagingList(pageSize).setFetchAhead(false).getPage(page);
	}
	public void setId(Integer d) {
		id= d;
	}
	public Integer getId() {
		return id;
	}
	public void setDrive_time(Integer d) {
		drive_time= d;
	}
	public Integer getDrive_time() {
		return drive_time;
	}
	public void setCustomer(Customer d) {
		customer= d;
	}
	public Customer getCustomer() {
		return customer;
	}
	public void setRoad_line(String d) {
		road_line= d;
	}
	public String getRoad_line() {
		return road_line;
	}
	public void setDriver(Driver d) {
		driver= d;
	}
	public Driver getDriver() {
		return driver;
	}
	public void setVehicle(Vehicle d) {
		vehicle= d;
	}
	public Vehicle getVehicle() {
		return vehicle;
	}
	public void setCheck_time(Integer d) {
		check_time= d;
	}
	public Integer getCheck_time() {
		return check_time;
	}
	public static Check_work findById(Integer d) {
		return find.byId(d);
	}
	public static String getIdJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Check_work v: find.all()) {
			node.put(v.getId().toString(), v.getId().toString()); 
		}
		return node.toString();
	}
}
